package com.itheima.sh.service;

import com.itheima.sh.dao.ContactMapper;
import com.itheima.sh.pojo.Contact;
import com.itheima.sh.utils.MybatisUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Author 张伟奇
 * @Date 2021/5/23 17:36
 * @Version 1.0
 **/
public class ContactService  {
    public List<Contact> findAllContact() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        ContactMapper mapper = sqlSession.getMapper(ContactMapper.class);
        List<Contact> list = mapper.findAllContact();
        sqlSession.close();
        return list;
    }

    public Contact loginContact(String name, String password) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        ContactMapper mapper = sqlSession.getMapper(ContactMapper.class);
        Contact contact = mapper.loginContact(name, password);
        return contact;
    }

    public Boolean registerContact(Contact c) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        ContactMapper mapper = sqlSession.getMapper(ContactMapper.class);
        Integer integer = mapper.registerContact(c);
        return integer>0;
    }
    public Contact findContactById(Integer id){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        ContactMapper mapper = sqlSession.getMapper(ContactMapper.class);
        Contact contact = mapper.findContactById(id);
        return contact;
    }
    public Boolean updateContact(Contact c) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        ContactMapper mapper = sqlSession.getMapper(ContactMapper.class);
        Integer integer = mapper.updateContact(c);
        return integer>0;
    }

    public Boolean deleteContactById(Integer id){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        ContactMapper mapper = sqlSession.getMapper(ContactMapper.class);
        Integer integer = mapper.deleteContactById(id);
        return  integer>0;
    }

}
